//
//  LunBo.m
//  自动轮播联系
//
//  Created by whz on 2017/7/10.
//  Copyright © 2017年 王鸿政_whz. All rights reserved.
//

#import "LunBo.h"
#define kUIScreenWidth [UIScreen mainScreen].bounds.size.width
#define kViewWidth self.frame.size.width
#define kViewHeight self.frame.size.height

@interface LunBo () <UIScrollViewDelegate>

@property(nonatomic, strong)UIScrollView *scrollViewDisplayImg;
@property(nonatomic, strong)UIImageView *imageViewImg1;
@property(nonatomic, strong)UIImageView *imageViewImg2;
@property(nonatomic, strong)UIImageView *imageViewImg3;
@property(nonatomic, strong)NSTimer *timer;
@property(nonatomic, strong)UIPageControl *pageControl;

@end

@implementation LunBo
{
    NSInteger _arrayIndex;
    CGFloat _contentOffsetX;
}

+ (instancetype)displayImgView:(NSArray *)arrayImgs {

    LunBo *lunboView = [[self alloc] initWithFrame:CGRectMake(0, 20, kUIScreenWidth, 200)];
    lunboView.arrayImgs = arrayImgs;
    
    return lunboView;
}

- (instancetype)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]) {
        [self createUI];
    }
    return self;
}

#pragma mark - 初始化UI

- (void)createUI {

    // MARK: - UIScrollView
    [self createScrollView];
    
    // MARK: - UIImageView
    [self createImageView];
    
    // MARK: - UIPageControl
    [self createPageControl];
    
    // MARK: - NSTimer
    [self createTimer];
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {

    [_timer invalidate]; // 方法1 关闭定时
    _timer = nil; // 方法1 释放
    
//    [_timer setFireDate:[NSDate distantFuture]]; // 方法2 取消定时
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {

    [self createTimer]; // 方法1
    
//    [_timer setFireDate:[NSDate distantPast]]; // 方法2 开启定时
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    [self goWithDirection];
    
    [_scrollViewDisplayImg setContentOffset:CGPointMake(self.frame.size.width, 0) animated:NO];
    
    [self changeImgs];
    
//    _contentOffsetX = _scrollViewDisplayImg.contentOffset.x;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    
}

#pragma mark - method

/**
 设置图片
 */
- (void)setArrayImgs:(NSArray *)arrayImgs {

    _arrayImgs = arrayImgs;
    
    [self changeImgs];
}

/**
 自动轮播方法
 */
- (void)timerAgain {

    [UIView animateWithDuration:0.25 animations:^{
        _scrollViewDisplayImg.contentOffset = CGPointMake(kViewWidth*2, 0);
    }];

    if (_arrayIndex < _arrayImgs.count) {
        _arrayIndex ++;
        if (_arrayIndex >= _arrayImgs.count) {
            _arrayIndex = 0;
        }
    }
    
    [self changeImgs];
    _scrollViewDisplayImg.contentOffset = CGPointMake(kViewWidth, 0);
}

/**
 滑动方向
 */
- (void)goWithDirection {

    if (_scrollViewDisplayImg.contentOffset.x == _contentOffsetX) {
        
    } else if (_scrollViewDisplayImg.contentOffset.x > _contentOffsetX) {
        
        _arrayIndex ++;
        
        if (_arrayIndex >= _arrayImgs.count) {
            _arrayIndex = 0;
        }
    } else {
        
        _arrayIndex --;
        
        if (_arrayIndex < 0) {
            _arrayIndex = _arrayImgs.count-1;
        }
    }
}

/**
 图片变换
 */
- (void)changeImgs {

    if (_arrayImgs.count == 0) {return;}
    
    if (_arrayImgs.count == 1) {
        
    } else if (_arrayImgs.count == 2) {
        
    } else if (_arrayImgs.count >= 3) {
        
        _imageViewImg2.image = _arrayImgs[_arrayIndex];
        
        if (_arrayIndex+1 < _arrayImgs.count) {
            _imageViewImg3.image = _arrayImgs[_arrayIndex+1];
        } else {
            _imageViewImg3.image = _arrayImgs[0];
        }
        
        if (_arrayIndex-1 >= 0) {
            _imageViewImg1.image = _arrayImgs[_arrayIndex-1];
        } else {
            _imageViewImg1.image = _arrayImgs[_arrayImgs.count-1];
        }
    }
}

/**
 创建UIScrollView
 */
- (void)createScrollView {

    self.scrollViewDisplayImg = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kViewWidth, kViewHeight)];
    [self addSubview:_scrollViewDisplayImg];
    _scrollViewDisplayImg.backgroundColor = [UIColor colorWithRed:231/255.0 green:231/255.0 blue:231/255.0 alpha:1.0];
    _scrollViewDisplayImg.contentSize = CGSizeMake(kViewWidth*3, kViewHeight);
    _scrollViewDisplayImg.contentOffset = CGPointMake(kViewWidth, 0);
    _scrollViewDisplayImg.bounces = NO;
    _scrollViewDisplayImg.pagingEnabled = YES;
    _scrollViewDisplayImg.showsHorizontalScrollIndicator = YES;
    _scrollViewDisplayImg.showsVerticalScrollIndicator = NO;
    
    _scrollViewDisplayImg.delegate = self;
    
    // MARK: - _contentOffsetX、_arrayIndex初始化
    
    _contentOffsetX = _scrollViewDisplayImg.contentOffset.x;
    _arrayIndex = 0;
}

/**
 创建图片循环的UIImageView
 */
- (void)createImageView {

    self.imageViewImg1 = [[UIImageView alloc] initWithFrame:CGRectMake(kViewWidth*0, 0, kViewWidth, kViewHeight)];
    self.imageViewImg2 = [[UIImageView alloc] initWithFrame:CGRectMake(kViewWidth*1, 0, kViewWidth, kViewHeight)];
    self.imageViewImg3 = [[UIImageView alloc] initWithFrame:CGRectMake(kViewWidth*2, 0, kViewWidth, kViewHeight)];
    [_scrollViewDisplayImg addSubview:_imageViewImg1];
    [_scrollViewDisplayImg addSubview:_imageViewImg2];
    [_scrollViewDisplayImg addSubview:_imageViewImg3];
}

/**
 创建UIPageControl
 */
- (void)createPageControl {

    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, kViewHeight-20, kViewWidth, 20)];
    [self addSubview:_pageControl];
    _pageControl.currentPage = 0;
    _pageControl.numberOfPages = 5;
    _pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
    _pageControl.pageIndicatorTintColor = [UIColor whiteColor];
}

/**
 创建NSTimer
 */
- (void)createTimer {

    self.timer = [NSTimer timerWithTimeInterval:3.0 target:self selector:@selector(timerAgain) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}
   
@end
